package edu.asu.sefcom.crimenet.search.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;


import com.mdimension.jchronic.Chronic;
import com.mdimension.jchronic.Options;
import com.mdimension.jchronic.utils.Span;


public class Util {

	public static void main(String args[]) {
		// new ExtractorImpl().execute();
//		String output = readFile("/home/dsm8/Documents/sefcom/Rus/zloy72.htm");
//		System.out.println(output);
//		System.out.println(commentFromText(" comments\"190> 10 comments</a>"));
	}

	public static String html2text(String html) {
		Logger logger = Logger.getLogger(Util.class);
		// logger.debug(message, t)
		return Jsoup.parse(html).text();
	}

	public static boolean isnull(Object article) {
		boolean test1=(null == article);
		if(article instanceof String && !test1){
			test1="".equals((String)article);
		}
		return test1;
	}
	public static String dateFromString(String html) {
		Options op = new Options();
		// op.setCompatibilityMode(true);
		// System.out.println(html);
		// html="tomorrow at 5pm";
		Span timec = Chronic.parse(html, op);
		// System.out.println("dsf "+timec);
		return "" + timec.toString();
	}

	public static String numberFromText(String html) {
		Pattern p = Pattern.compile("-?\\d+");
		Matcher m = p.matcher(html);
		String res = "";
		while (m.find()) {
			res = m.group();
		}
		return res;
	}

	public static int extractUrls(String input) {
		int result = extractUrls111(input, null);
		if (result == 0) {
			String testrun = input;
			if (input.startsWith("(") && input.endsWith(")")) {
				testrun = input.substring(1, input.length() - 2);
			}
			String pattern1 = "\\(?\bhttp://[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]/";
			result = extractUrls111(input, pattern1);
		}
		return result;
	}

	static int extractUrls111(String input, String pattern1) {
		List<String> result = new ArrayList<String>();
		Pattern pattern = null;
		if (pattern1 == null) {
			pattern = Pattern
					.compile("\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)"
							+ "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov"
							+ "|mil|biz|info|mobi|name|aero|jobs|museum"
							+ "|travel|[a-z]{2}))(:[\\d]{1,5})?"
							+ "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?"
							+ "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?"
							+ "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)"
							+ "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?"
							+ "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*"
							+ "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
		} else {
			pattern = Pattern.compile(pattern1);
		}
		Matcher matcher = pattern.matcher(input);
		while (matcher.find()) {
			result.add(matcher.group());
		}

		return result.size();
	}

	public static String readFile(String fileName)
			throws IllegalArgumentException {
		String docData = "";
		FileInputStream inStream = null;
		try {
			File file = new File(fileName);
			String charsetName = "UTF-8";
			// String charsetName = "windows-1251";
			inStream = new FileInputStream(file);
			MappedByteBuffer byteData = inStream.getChannel().map(
					FileChannel.MapMode.READ_ONLY, 0, file.length());
			docData = Charset.forName(charsetName).decode(byteData).toString();
			inStream.close();
		} catch (Exception e) {
			System.out.println("fileName: "+fileName);
			throw new IllegalArgumentException(e.getLocalizedMessage());
		}

		// Logger.getLogger(Util.class).debug("---$$$---" + docData);
		return docData;
	}

	
	public static String commentFromText(String content) {
		// TODO Auto-generated method stub
		String res1 = "";
		try {
//			Pattern p = Pattern.compile("<a class=\"comments\".*</a>.*</a>",
//					Pattern.DOTALL);
			Pattern p = Pattern.compile("comments\".*comments</a>",Pattern.DOTALL);
			Matcher m = p.matcher(content);
			boolean result = m.find();
			String res = "";
			if (result) {
				res = m.group();
			}
			res1 = Util.numberFromText(Util.html2text(res));
			if(Integer.valueOf(res1)>15){
				res1="0";
			}
		} catch (Exception e) {
		}
		return res1;
	}

	public String useRegexForPost(String fileData, String regex, int fromInd) {
		String res = "";
		try {
			String part1 = fileData.substring(fromInd);
			Pattern p = Pattern.compile(regex, Pattern.DOTALL);
			Matcher m = p.matcher(part1);

			if (m.find()) {
				res = m.group();
			}

		} catch (Exception e) {
		}
		return res;
	}

	public static String extractField(String post, String start, String end) {
		int beginIndex = post.indexOf(start);
		int endIndex = post.indexOf(end, beginIndex);
		int endlength = end.length();
		String postData = "";
		try {
			if (beginIndex != -1) {
				postData = post.substring(beginIndex, endIndex + endlength);
			}
		} catch (Exception e) {
			// System.out.println(e.getLocalizedMessage());
			// System.exit(0);
		}
		return postData;
	}
	
	public static List<String> returnUsersForparent(String parent){
		String[] argsbhcrew = new String[] { "90h", "agregatore", "alex",
				"andy", "ark", "bhcrew", "break", "bub", "buk", "bz", "cgi",
				"chaotist", "cold", "count", "cqi", "crash", "cut", "cyb",
				"cyber", "d", "d5d", "daid", "dark", "delight", "devil", "dg",
				"dmnt", "domestic", "drn", "enkrypt", "exit", "fill", "fluro",
				"generator", "glukkers", "guman", "hellman", "impud", "iret",
				"ispace", "its", "jack", "jd", "kalo", "klaster", "kolen",
				"krash", "kyp", "la", "aur", "lamer", "lj", "lucifer",
				"lutifer", "magic2k", "master", "mc", "med", "misha", "moi",
				"moon", "mor", "mrbuggers", "mr_hukker", "mummi", "n1k",
				"nait", "nitrex", "nomad", "noxp", "ns", "ocetp", "odept",
				"oskar", "panterka", "pedestrian", "perajok", "piska", "pitro",
				"pizza", "privac", "ram", "redhat", "redo", "renny", "sham",
				"stden", "sunx", "sww", "tim", "x90", "xs", "yen", "yuzvir",
				"z", "zdeus", "zenit", "zeppe1in", "zhen", "zloi" };
		String[] argscup = new String[] { "777bs", "c0der", "danila", "kiote",
				"lisnake", "moreno", "profit", "ww", "yi", "bzmrnkt", "cupsu",
				"kamarez", "lamarez", "mast3r", "panterka", "smix", "xcoder",
				"zaco" };
		String[] argsdamagelab = new String[] { "4kella", "357", "1983",
				"allotus", "brutt", "damagelab", "deface", "denchis", "dinar",
				"dvix", "ettee", "gam", "gr3", "ikso", "jerry", "kotuke",
				"n1d", "n1k", "psicho", "rev", "rubi", "the2kid", "winux",
				"xs", "zavu", "zd", "zx" };
		String[] argsmazafaka = new String[] { "beyond", "cr4sh", "d5d",
				"da99er", "daimik", "darlingkicks", "drnimnul", "ew4rk",
				"ignis_et_ferro", "marozz", "mazafaka_group", "nm", "r4dik",
				"rossoman", "spee_ua" };
		String[] argsruhack = new String[] { "bedr0s", "cr4sh", "eas7",
				"gruz0", "kirsanov_anton", "puzanov", "rstghc",
				"ru_hack_group", "stden", "vv6" };
		Map<String, String[]> map1=new HashMap<String, String[]>();
		map1.put("bhcrew", argsbhcrew);map1.put("mazafaka", argsmazafaka);
		map1.put("damagelab", argsdamagelab);map1.put("ru_hack", argsruhack);
		map1.put("CUP", argscup);
		String[] users1=map1.get(parent);
		return Arrays.asList(users1); 
	}
	public static List<String> getYearList(){
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream("config/app.properties"));
		} catch (IOException e) {
		}
		String yearList = "" + properties.getProperty("edu.asu.cidse.sefcom.crimenet.indexing.yearlist");
		String[] list1=yearList.split("[,]");
//		Logger.getLogger(Util.class).warn(list1);
		return Arrays.asList(list1);
	}
}
